package com.example.databasepj.service;

import com.example.databasepj.Request.DishDeleteRequest;
import com.example.databasepj.Request.DishInsertRequest;
import com.example.databasepj.Request.DishUpdateRequest;
import com.example.databasepj.Request.UserReviewDishRequest;
import com.example.databasepj.dto.DishReviewResponse;
import com.example.databasepj.dto.HistoryPriceResponse;
import com.example.databasepj.entity.DishEntity;
import com.example.databasepj.entity.DishReviewEntity;
import com.example.databasepj.entity.HistoryPriceEntity;
import com.example.databasepj.exception.BusinessException;
import com.example.databasepj.exception.ErrorCode;
import com.example.databasepj.mapper.DishMapper;
import com.example.databasepj.mapper.DishReviewMapper;
import com.example.databasepj.mapper.HistoryPriceMapper;
import org.apache.ibatis.exceptions.PersistenceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private HistoryPriceMapper historyPriceMapper;
    @Autowired
    private DishReviewMapper dishReviewMapper;

    public List<DishEntity> selectDishByMerchantIdAndSignature(int id){
        return dishMapper.selectDishByMerchantIdAndSignature(id);
    }
    public List<DishEntity> selectDishByMerchantId (int id){
        return dishMapper.selectDishByMerchantId(id);
    }
    public DishEntity selectDishByDishId(int id){
        return dishMapper.selectDishByDishId(id);
    }
    public List<HistoryPriceResponse> selectPriceByDishID(int id){
        List<HistoryPriceEntity> historyPriceEntities = historyPriceMapper.selectPriceByDishId(id);
        List<HistoryPriceResponse> historyPriceResponses = new ArrayList<>();
        for (int i=0;i<historyPriceEntities.size();i++){
            HistoryPriceEntity price=historyPriceEntities.get(i);
            historyPriceResponses.add(new HistoryPriceResponse(price.getPrice(),price.getModify_time()));
        }
        return historyPriceResponses;
    }

    public List<DishReviewResponse> selectReviewByDishId(int id){
        List<DishReviewEntity> dishReviewEntities = dishReviewMapper.selectReviewByDishId(id);
        List<DishReviewResponse> dishReviewResponses=new ArrayList<>();
        for (int i=0;i<dishReviewEntities.size();i++){
            DishReviewEntity review =dishReviewEntities.get(i);
            dishReviewResponses.add(new DishReviewResponse(review.getDish_review_id(),review.getUser_id(),review.getRating(), review.getComment()));
        }
        return dishReviewResponses;
    }

    //用户评价菜品
    public void insertDishReview(UserReviewDishRequest request){
        DishReviewEntity dishReviewEntity=new DishReviewEntity(request.getUser_ID(), request.getDish_ID(), request.getOrder_ID());
        dishReviewEntity.setComment(request.getComment());
        dishReviewEntity.setRating(request.getRating());
        //该订单中对该菜品的评价是否已存在
        DishReviewEntity dishReview=dishReviewMapper.selectAllByOrder_idAndDish_id(dishReviewEntity.getOrder_id(),dishReviewEntity.getDish_id());
        //如果存在，则更新
        if(dishReview != null){
            dishReviewEntity.setDish_review_id(dishReview.getDish_review_id());
            dishReviewMapper.updateDishReview(dishReviewEntity);
        }
        else {
            try{
                dishReviewMapper.insertDishReview(dishReviewEntity);

            }catch(PersistenceException e){
               // return new UserReviewMerchantResponse("fail","评价提交失败");
                   throw new BusinessException(ErrorCode.SYSTEM_ERROR);
            }
        }
    }

    public void insertDish(DishInsertRequest request){
        DishEntity dish = new DishEntity(request.getMerchant_ID(),request.getDish_category(),request.getDish_name());
        dish.setIsSignatureDish(request.getIsSignatureDish());
        dish.setAllergen(request.getAllergen());
        dish.setNutritionInfo(request.getNutritionInfo());
        dish.setDescription(request.getDescription());
        dish.setIngredient(request.getIngredient());
        dish.setPrice(request.getPrice());
        dishMapper.insertDish(dish);
        LocalDateTime now = LocalDateTime.now();
        HistoryPriceEntity historyPrice = new HistoryPriceEntity(dish.getDish_id(),dish.getPrice(),now);
        historyPriceMapper.insertHistoryPrice(historyPrice);
    }

    public void updateDish(DishUpdateRequest request){
        //注意判断是否要更新historyPrice
        DishEntity dish = new DishEntity(request.getMerchant_ID(),request.getDish_category(),request.getDish_name());
        dish.setIsSignatureDish(request.getIsSignatureDish());
        dish.setAllergen(request.getAllergen());
        dish.setNutritionInfo(request.getNutritionInfo());
        dish.setDescription(request.getDescription());
        dish.setIngredient(request.getIngredient());
        dish.setPrice(request.getPrice());
        dish.setDish_id(request.getdish_ID());
        //先判断price是否有更新
        DishEntity oldDish = dishMapper.selectDishByDishId(dish.getDish_id());
        //如果price不变
        float oldPrice = oldDish.getPrice();
        float newPrice=dish.getPrice();
        if(oldPrice == newPrice ){
            dishMapper.updateDish(dish);
        }
        else {
            //先更新priceHistory
            LocalDateTime now = LocalDateTime.now();
            HistoryPriceEntity historyPrice = new HistoryPriceEntity(dish.getDish_id(), dish.getPrice(), now);
            historyPriceMapper.insertHistoryPrice(historyPrice);
            //再更新dish
            dishMapper.updateDish(dish);
        }

    }
    public void deleteDish(DishDeleteRequest request){
        dishMapper.deleteDish(request.getDish_ID(), request.getMerchant_ID());
    }


}
